﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Text;
using System.Text.RegularExpressions;
using ExtremeML.Core;

namespace ExtremeML.Spreadsheet.HeaderFooter
{
	/// <summary>
	/// Extension methods to apply common functionality to classes that implement <see cref="IHeaderFooter"/>.
	/// </summary>
	internal static class HeaderFooterExtensions
	{
		/// <summary>
		/// Returns the section string that match the specified filter.
		/// </summary>
		public static string GetSectionString(
			this IHeaderFooter wrapper,
			HeaderFooterSection filter)
		{
			if (string.IsNullOrEmpty(wrapper.Value)) return string.Empty;

			const string template = @"(?<data>(?<!&)(?<=&{0})[\w\W]+?(?=&C|&R|&L|$))";

			var pattern = string.Format(template, "LCR"[(int)filter]);
			var builder = new StringBuilder();

			foreach (Match match in Regex.Matches(wrapper.Value, pattern))
			{
				if (match.Groups["data"].Success)
				{
					builder.Append(match.Value);
				}
			}

			return builder.ToString();
		}

		/// <summary>
		/// Sets the specified section string to the passed value.
		/// </summary>
		public static void SetSectionString(
			this IHeaderFooter wrapper,
			HeaderFooterSection filter,
			string value)
		{
			if (value == null) value = string.Empty;

			var left = GetSectionString(wrapper, HeaderFooterSection.Left);
			var center = GetSectionString(wrapper, HeaderFooterSection.Center);
			var right = GetSectionString(wrapper, HeaderFooterSection.Right);

			switch (filter)
			{
				case HeaderFooterSection.Left:
					left = value;
					break;
				case HeaderFooterSection.Center:
					center = value;
					break;
				case HeaderFooterSection.Right:
					right = value;
					break;
				default:
					break;
			}

			left = string.IsNullOrEmpty(left) ? string.Empty : "&L" + left;
			center = string.IsNullOrEmpty(center) ? string.Empty : "&C" + center;
			right = string.IsNullOrEmpty(right) ? string.Empty : "&R" + right;

			wrapper.Value = left + center + right;
		}

		/// <summary>
		/// Populates the string format placeholders in the specified section using the passed array of object values.
		/// </summary>
		public static void PopulateSectionString(
			this IHeaderFooter wrapper,
			HeaderFooterSection filter,
			params object[] values)
		{
			var value = Utility.SafeFormat(GetSectionString(wrapper, filter), values);
			SetSectionString(wrapper, filter, value);
		}
	}
}
